package com.dinzeer.legendreliclib.lib.util.slashblade;

import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.registry.SpecialEffectsRegistry;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Collection;
import java.util.List;

/**
 * 特殊效果的抽象基类
 * 提供统一的事件处理框架，子类只需实现具体的处理逻辑
 */

public abstract class AbstractSpecialEffect extends SpecialEffect {

    /**
     * 构造函数
     * @param questionLevel 效果等级
     */
    public AbstractSpecialEffect(int questionLevel) {
        super(questionLevel);
    }

    // ==================== 事件处理方法 ====================

    /**
     * 处理实体受伤事件
     * 当玩家用刀攻击实体造成伤害时触发
     */
    @SubscribeEvent
    public static void onHitEntity(LivingHurtEvent event) {
        if (!(event.getSource().getEntity() instanceof LivingEntity  player)) return;

        processEvent(player, (effect, attacker) ->
                effect.handleLivingHurt(event, attacker)
        );

    }

    /**
     * 处理刀剑更新事件
     * 每tick调用，用于持续效果和状态更新
     */
    @SubscribeEvent
    public static void onUpdate(SlashBladeEvent.UpdateEvent event) {
        if (!(event.getEntity() instanceof LivingEntity lv)) return;
        processEvent(lv, (effect, attacker) ->
                effect.handleUpdate(event, attacker)
        );
    }

    /**
     * 处理刀剑击中实体事件
     * 当刀剑成功击中实体时触发
     */
    @SubscribeEvent
    public static void onHitEntity(SlashBladeEvent.HitEvent event) {
        processEvent(event.getUser(), (effect, attacker) ->
                effect.handleHitEntity(event, attacker)
        );
    }

    /**
     * 处理挥刀事件
     * 当玩家执行挥刀动作时触发
     */
    @SubscribeEvent
    public static void onDoSlash(SlashBladeEvent.DoSlashEvent event) {
        processEvent(event.getUser(), (effect, attacker) ->
                effect.handleDoSlash(event, attacker)
        );
    }

    /**
     * 处理召唤剑击中实体事件
     * 当召唤剑击中实体时触发
     */
    @SubscribeEvent
    public static void onSummonedSwordHit(SlashBladeEvent.SummonedSwordOnHitEntityEvent event) {
        if (!(event.getSummonedSword().getOwner() instanceof LivingEntity lv)) return;
        processEvent(lv, (effect, attacker) ->
                effect.handleSummonedSwordHit(event, attacker)
        );
    }

    // ==================== 辅助方法 ====================

    /**
     * 统一的事件处理逻辑
     * @param entity 可能持有刀剑的实体
     * @param handler 具体的事件处理器
     */
    private static void processEvent(LivingEntity entity, EffectHandler handler) {
        // 检查实体是否有效且持有刀剑
        if (entity != null && entity.getMainHandItem().getItem() instanceof ItemSlashBlade) {
            // 获取刀剑上的所有特殊效果

            Collection<ResourceLocation> effects = SlashBladeUtil.getState(entity.getMainHandItem()).getSpecialEffects();

            // 遍历所有效果并处理
            for (ResourceLocation effectId : effects) {
                SpecialEffect effect = SpecialEffectsRegistry.REGISTRY.get().getValue(effectId);
                if (effect instanceof AbstractSpecialEffect abstractEffect) {
                    handler.handle(abstractEffect, entity);
                }
            }
        }
    }

    /**
     * 效果处理器函数式接口
     */
    @FunctionalInterface
    private interface EffectHandler {
        void handle(AbstractSpecialEffect effect, LivingEntity attacker);
    }

    // ==================== 抽象方法 - 子类必须实现 ====================

    /**
     * 处理实体受伤事件的具体逻辑
     */
    public  void handleLivingHurt(LivingHurtEvent event, LivingEntity attacker){};

    /**
     * 处理刀剑更新事件的具体逻辑
     */
    public  void handleUpdate(SlashBladeEvent.UpdateEvent event, LivingEntity attacker){};

    /**
     * 处理刀剑击中实体事件的具体逻辑
     */
    public  void handleHitEntity(SlashBladeEvent.HitEvent event, LivingEntity attacker){};

    /**
     * 处理挥刀事件的具体逻辑
     */
    public  void handleDoSlash(SlashBladeEvent.DoSlashEvent event, LivingEntity attacker){};

    /**
     * 处理召唤剑击中实体事件的具体逻辑
     */
    public  void handleSummonedSwordHit(SlashBladeEvent.SummonedSwordOnHitEntityEvent event, LivingEntity attacker){};


    public Boolean isEffective(LivingEntity attacker){
        int level;
        if (attacker instanceof Player player){
            level = player.experienceLevel;
        }else {
            level=this.getRequestLevel();
        }
        return isEffective(this, level);
    }







}